package fr.poy.mygluco.client.ui;

import com.extjs.gxt.ui.client.GXT;
import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.binding.FormBinding;
import com.extjs.gxt.ui.client.data.BeanModelFactory;
import com.extjs.gxt.ui.client.data.BeanModelLookup;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.widget.TabItem;
import com.extjs.gxt.ui.client.widget.Window;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.FormButtonBinding;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.NumberField;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.layout.FormLayout;
import com.google.gwt.user.client.ui.TabPanel;

import fr.poy.mygluco.client.App;
import fr.poy.mygluco.model.UserProfile;

public final class ProfileSettings extends Window {
	
	private static ProfileSettings instance = new ProfileSettings();
	
	public static ProfileSettings getInstance(){
		return instance;
	}

	private ProfileSettings() {
 
		this.setModal(true);
		this.setBlinkModal(true);
		this.setHeading(App.getMessages().settingsDialogTitle());
		this.setIconStyle("toolbar-settings");
		this.setSize(410, 340);
		this.setLayout(new FitLayout());
		//------------------------------------------
		FormPanel form = new FormPanel();
		form.setHeaderVisible(false);
		form.setPadding(0);  
		form.setFrame(false);
		form.setBodyBorder(false);  
		FormLayout fl = new FormLayout();
		fl.setLabelWidth(150);
		form.setStyleAttribute("padding", "10px");  
		form.setLayout(fl/*new FitLayout()*/);  
		 
		
//		TabPanel tabs = new TabPanel(); 
//		
//		TabItem general = new TabItem();   
//		general.setStyleAttribute("padding", "10px");  
//		FormLayout fl = new FormLayout();
//		fl.setLabelWidth(150);
//		general.setLayout(fl);  
		 
		TextField<String> timeZone = new TextField<String>();
		timeZone.setName("timeZone");  
		timeZone.setFieldLabel(App.getMessages().settingsTimeZone());
		timeZone.setAllowBlank(false);  
		form.add(timeZone);
		
		TextField<String> dayFormat = new TextField<String>();
		dayFormat.setName("dayFormat");  
		dayFormat.setFieldLabel(App.getMessages().settingsDayFormat());
		dayFormat.setAllowBlank(false);  
		form.add(dayFormat);
		
		TextField<String> dateFormat = new TextField<String>();
		dateFormat.setName("dateFormat");  
		dateFormat.setFieldLabel(App.getMessages().settingsDateFormat());
		dateFormat.setAllowBlank(false);  
		form.add(dateFormat);
		
		TextField<String> timeFormat = new TextField<String>();
		timeFormat.setName("timeFormat");  
		timeFormat.setFieldLabel(App.getMessages().settingsTimeFormat());
		timeFormat.setAllowBlank(false);  
		form.add(timeFormat);
		
		TextField<String> bgUnit = new TextField<String>();
		bgUnit.setName("bgUnit");  
		bgUnit.setFieldLabel(App.getMessages().settingsBgUnit());
		bgUnit.setAllowBlank(false);  
		form.add(bgUnit);
		
		NumberField bgHigh = new NumberField();
		bgHigh.setPropertyEditorType(Integer.class);
		bgHigh.setName("bgHigh");  
		bgHigh.setFieldLabel(App.getMessages().settingsBgHigh());
		bgHigh.setAllowBlank(false);  
		bgHigh.setAllowNegative(false); 
		//bgHigh.setDecimalSeparator("."); 
		form.add(bgHigh);
		
		NumberField bgLow = new NumberField();
		bgLow.setPropertyEditorType(Integer.class);
		bgLow.setName("bgLow");  
		bgLow.setFieldLabel(App.getMessages().settingsBgLow());
		bgLow.setAllowBlank(false);  
		bgLow.setAllowNegative(false);
		form.add(bgLow);
		
		NumberField limitHypo = new NumberField();
		limitHypo.setPropertyEditorType(Integer.class);
		limitHypo.setName("limitHypo");  
		limitHypo.setFieldLabel(App.getMessages().settingsLimitHypo());
		limitHypo.setAllowBlank(false);  
		limitHypo.setAllowNegative(false);
		form.add(limitHypo); 
		
		
//		TabItem xmpp = new TabItem();  
//		xmpp.setStyleAttribute("padding", "10px");  
//		FormLayout fl2 = new FormLayout();
//		fl2.setLabelWidth(150);
//		xmpp.setLayout(fl2);  
		
		TextField<String> xmppPattern = new TextField<String>();
		xmppPattern.setName("xmppPattern");  
		xmppPattern.setFieldLabel(App.getMessages().settingsXmppPattern());
		xmppPattern.setAllowBlank(false);  
		form.add(xmppPattern);
		
		
		
//		tabs.add(general,"ggg");
//		tabs.add(xmpp,"xmpp");
//		form.add(tabs); 
		
		//---------------------- 
		BeanModelFactory factory = BeanModelLookup.get().getFactory(UserProfile.class);
		FormBinding fbinding = new FormBinding(form);  
		ModelData m = factory.createModel(App.getUserProfile());
		fbinding.autoBind();  
		fbinding.bind(m);  
		
		//-------------------------------------------
		Button b = new Button(GXT.MESSAGES.messageBox_ok(),new SelectionListener<ButtonEvent>() {
			public void componentSelected(ButtonEvent ce) { 
				App.saveProfile(); 
				instance.hide();
			}
		});
		form.addButton(b);
		
		form.addButton(new Button(GXT.MESSAGES.messageBox_cancel()
				,new SelectionListener<ButtonEvent>() {
			public void componentSelected(ButtonEvent ce) { 
				instance.hide();
			}
		}));

		form.setButtonAlign(HorizontalAlignment.CENTER); 
		FormButtonBinding binding = new FormButtonBinding(form);
		binding.addButton(b); 
		this.add(form);

	}

}
